package com.egao.common.system.biz.impl;

import com.egao.common.core.web.BaseDao;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.biz.UserBiz;
import com.egao.common.system.entity.User;
import com.egao.orderB.entity.Customer;
import org.checkerframework.checker.units.qual.A;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-05-27 15:04:13
 * @version ：1.0
 */
@Repository
public class UserBizImpl extends BaseDao implements UserBiz {


	@Override
	public User apiGetByCustomerId(String id, String type) {
		RowMapper<User> rm;
		rm = new BeanPropertyRowMapper<User>(User.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select u.user_id,u.company_id,u.username");
		sql.append(",u.customer_id");
		sql.append(",CASE u.type WHEN '司机' THEN (select d.name from chf_driver d where d.id=u.customer_id) ");
		sql.append(" WHEN '客户' THEN (select c.name from chf_customer c where c.id=u.customer_id) ELSE u.username END as customerName ");
		sql.append(",u.password,u.nick_name,u.avatar,u.sex");
		sql.append(",u.phone,u.email,u.email_verified,u.true_name");
		sql.append(",u.id_card,u.birthday,u.introduction,u.organization_id");
		sql.append(",u.type,u.state,u.create_time,u.update_time ");
		sql.append(" from sys_user u ");
		sql.append(" where u.deleted=0 and u.customer_id=:customer_id ");
		map.put("customer_id", id);
		sql.append(" and u.type=:type ");
		map.put("type", type);
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}
	@Override
	public User getUserById(User user) {
		RowMapper<User> rm = new BeanPropertyRowMapper<User>(User.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select u.user_id,u.company_id,u.username");
		sql.append(",u.customer_id");
		sql.append(",CASE u.type WHEN '司机' THEN (select d.name from chf_driver d where d.id=u.customer_id) ");
		sql.append(" WHEN '客户' THEN (select c.name from chf_customer c where c.id=u.customer_id) ELSE u.username END as customerName ");
		sql.append(",u.password,u.nick_name,u.avatar,u.sex");
		sql.append(",u.phone,u.email,u.email_verified,u.true_name");
		sql.append(",u.id_card,u.birthday,u.introduction,u.organization_id");
		sql.append(",u.type,u.admin,u.state,u.create_time,u.update_time");
		sql.append(",cmy.name companyName ");
		sql.append(" from sys_user u ");
		sql.append(" inner join chf_company cmy on cmy.id=u.company_id ");
		sql.append(" where u.deleted=0 and cmy.deleted=0 and u.user_id=:id ");
		map.put("id", user.getUserId()+"");
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}
	@Override
	public User getUserByPhone(User user) {
		RowMapper<User> rm = new BeanPropertyRowMapper<User>(User.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select u.user_id,u.company_id,u.username");
		sql.append(",u.customer_id");
		sql.append(",CASE u.type WHEN '司机' THEN (select d.name from chf_driver d where d.id=u.customer_id) ");
		sql.append(" WHEN '客户' THEN (select c.name from chf_customer c where c.id=u.customer_id) ELSE u.username END as customerName ");
		sql.append(",u.password,u.nick_name,u.avatar,u.sex");
		sql.append(",u.phone,u.email,u.email_verified,u.true_name");
		sql.append(",u.id_card,u.birthday,u.introduction,u.organization_id");
		sql.append(",u.type,u.admin,u.state,u.create_time,u.update_time");
		sql.append(",cmy.name companyName ");
		sql.append(" from sys_user u ");
		sql.append(" inner join chf_company cmy on cmy.id=u.company_id ");
		sql.append(" where u.deleted=0 and cmy.deleted=0 and u.phone=:phone ");
		map.put("phone", user.getPhone()+"");
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public User getUserByUsername(User user) {
		RowMapper<User> rm;
		rm = new BeanPropertyRowMapper<User>(User.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select u.user_id,u.company_id,u.username");
		sql.append(",u.customer_id");
		sql.append(",CASE u.type WHEN '司机' THEN (select d.name from chf_driver d where d.id=u.customer_id) ");
		sql.append(" WHEN '客户' THEN (select c.name from chf_customer c where c.id=u.customer_id) ELSE u.username END as customerName ");
		sql.append(",u.password,u.nick_name,u.avatar,u.sex");
		sql.append(",u.phone,u.email,u.email_verified,u.true_name");
		sql.append(",u.id_card,u.birthday,u.introduction,u.organization_id");
		sql.append(",u.type,u.state,u.create_time,u.update_time,u.deleted ");
		sql.append(",cmy.name companyName ");
		sql.append(" from sys_user u ");
		sql.append(" inner join chf_company cmy on cmy.id=u.company_id ");
		sql.append(" where cmy.deleted=0 and u.username=:username ");
		map.put("username", user.getUsername());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public User apiGetByUsername(String username) {
		RowMapper<User> rm;
		rm = new BeanPropertyRowMapper<User>(User.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select u.user_id,u.company_id,u.username");
		sql.append(",u.password,u.nick_name,u.avatar,u.sex");
		sql.append(",u.phone,u.email,u.email_verified,u.true_name");
		sql.append(",u.id_card,u.birthday,u.introduction,u.organization_id");
		sql.append(",u.type,u.state,u.create_time,u.update_time ");
		sql.append(" from sys_user u ");
		sql.append(" where u.deleted=0 and u.username=:username ");
		map.put("username", username);
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public Integer apiSave(User user) {
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sys_user (");
		sql.append("company_id," +
				"customer_id," +
				"username,password");
//		sql.append(",nick_name,avatar,sex,birthday");
		sql.append(",phone,type,admin,state");
		sql.append(") values ( ");
		sql.append(":company_id," +
				":customer_id," +
				":username,:password");
//		sql.append(",:nick_name,:avatar,:sex,:phone,:birthday");
		sql.append(",:phone,:type,:admin,:state");
		sql.append(")");
		map.put("company_id", user.getCompanyId() + "");
		map.put("customer_id", user.getCustomerId()==null?"":user.getCustomerId() + "");
		map.put("username", user.getUsername());
		map.put("password", user.getPassword());
		map.put("phone", user.getPhone());
		map.put("type", user.getType());
		map.put("admin", user.getAdmin());
		map.put("state", user.getState() + "");
		getMapParameter(sql, map);
		int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
			@Override
			public boolean hasValue(String paramName) {
				return map.containsKey(paramName);
			}
			@Override
			public Object getValue(String paramName) throws IllegalArgumentException {
				return map.get(paramName);
			}
		}, generatedKeyHolder);

		Integer id = generatedKeyHolder.getKey().intValue();
		return id;
	}
	@Override
	public boolean saveUserRole(User user) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sys_user_role (");
		sql.append("user_id,role_id) values ( ");
		sql.append(":user_id,:role_id");
		sql.append(")");
		map.put("user_id", user.getUserId() + "");
		map.put("role_id", user.getRoleId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
	@Override
	public boolean apiExamine(User user, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sys_user set ");
		sql.append("state=:state ");
		sql.append(",update_user=:update_user where user_id=:user_id ");
		map.put("state", user.getState()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("user_id", user.getUserId()+"");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updatePsw(User user, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sys_user set ");
		sql.append("password=:password ");
		sql.append(",update_user=:update_user,update_time=now() where user_id=:user_id ");
		map.put("password", user.getPassword());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("user_id", user.getUserId()+"");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean apiExamineCustomerId(User user, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sys_user set ");
		sql.append("state=:state ");
		sql.append(",update_user=:update_user where customer_id=:customer_id ");
		map.put("state", user.getState()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("customer_id", user.getCustomerId()+"");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean apiSetCustomerId(User user, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sys_user set ");
		sql.append("customer_id=:customer_id ");
		sql.append(" where user_id=:user_id ");
		map.put("customer_id", user.getCustomerId()==null?"":user.getCustomerId() + "");
		map.put("user_id", user.getUserId()+"");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
}
